<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Queue\Capsule\Manager;
use Session;
use DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Log;
use App\Http\Requests\CreateActivityRequest;
use App\Http\Requests\UpdateActivityRequest;
use App\Activity;
use Illuminate\Support\Facades\Auth;

class ActivityController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

	public function index(Request $request) {
		// 查看当前菜单可使用的功能按钮
		$uri = $request->path();
		$data['optionArr'] =  $this->checkMenuOption($uri);

		$activityObj = Activity::where('is_del',1)->get();
//		dd($activityObj);

		$data['activitys'] = $activityObj;

		return view('activity.list')->with($data);
	}

    public function create()
    {
        return view('activity.create');
    }


    public function store(CreateActivityRequest $request){
        $activityObj = new Activity();
	    $activityObj->activity_name =$request->activity_name;
	    $activityObj->activity_dec =$request->activity_dec;
	    $activityObj->author =Auth::user()->name;
	    $activityObj->remark =$request->remark;

        if($activityObj->save()){

	        $adminUser = Auth::user();
	        if($adminUser){
		        $userId = $adminUser->id;
		        $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').' 新增了活动：【  '.$request->activity_name.' 】';
		        adminOperationLog($request,$userId,$logs);
	        }

            return redirect('activity/list')->with('success','新增成功');
        }else{
            return redirect()->back()->withInput();
        }
    }


	public function show($id)
	{
		// 获取活动下的优惠券
		$activityCoupon = Activity::getActivityCoupon($id);
//		dd($activityCoupon);

		$data['activityCoupons'] = $activityCoupon;

		return view('activity.show')->with($data);

	}


    public function edit($id)
    {
	    $activityObj = Activity::find($id);
        $data['activityInfo'] = $activityObj;
//        dd($activityObj);

        return view('activity.edit')->with($data);

    }


    public function update(UpdateActivityRequest $request, $id){

	    $activityObj = Activity::find($id);
	    $activityObj->activity_name =$request->activity_name;
	    $activityObj->activity_dec =$request->activity_dec;
	    $activityObj->author =Auth::user()->name;
	    $activityObj->remark =$request->remark;

        if(!$activityObj->save()){
            return Redirect::back()->withInput()->withErrors(['fail' => trans('message.updateFail')]);
        }

	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'编辑了活动：【 '.$request->activity_name.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return Redirect::to(url('activity/list'))->with('success', trans('message.updateSuccess'));

    }

    public function delete(Request $request){
		$eid = $request->id;
        if(!$eid){
            return '-1';
        }

        //  del
//	    $affectId= Activity::where('id',$eid)->update([
//		    'is_del'=>-1
//	    ]);
	    $affectId = Activity::where('id',$eid)->delete();
        if(!$affectId){
            return -1;
        }

	    $name = Activity::getNameById($eid);
	    $adminUser = Auth::user();
	    if($adminUser){
		    $userId = $adminUser->id;
		    $logs = $adminUser->name.'在'.date('Y-m-d H:i:s').'删除了活动：【 '.$name.' 】';
		    adminOperationLog($request,$userId,$logs);
	    }

        return 1;

    }


}





